<template>
  <div class="app-container">
    <div class="searchContianer" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}"> 
      <div class="searchLeft" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}">
        <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="68px">
          
          <el-form-item label="师傅姓名" prop="realName" class="mb8">
            <el-input
              v-model="queryParams.realName"
              placeholder="请输入师傅姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile" class="mb8">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          
          <el-form-item label="服务类型" prop="serverTypeId" class="mb8">
            <el-select v-model="queryParams.serverTypeId" placeholder="请输入服务类型" clearable size="small">
              <el-option label="全部" value="" />
              <el-option v-for="un in serverTypes" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
          </el-form-item>

          <el-form-item label="产品类型" prop="productCategoryId" class="mb8">
            <el-select v-model="queryParams.productCategoryId" placeholder="请输入产品类型" clearable size="small">
              <el-option label="全部" value="" />
              <el-option v-for="un in productCategorys" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
          </el-form-item>

          <el-form-item label="服务状态" prop="serviceState" class="mb8">
            <el-select v-model="queryParams.serviceState" placeholder="请选择服务状态" clearable>
            <el-option label="全部" value="" />
              <el-option
                v-for="dict in dict.type.sk_engineer_server_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="账号状态" prop="status" class="mb8">
            <el-select v-model="queryParams.status" placeholder="请选择账号状态" clearable>
            <el-option label="全部" value="" />
              <el-option
                v-for="dict in dict.type.sk_common_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <!--
          <el-form-item label="省" prop="provinceId" class="mb8">
            <el-select v-model="queryParams.provinceId" placeholder="请选择省份" clearable size="small" @change="changeProvince">
              <el-option label="全部" value="" />
              <el-option v-for="un in provinceAreas" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
          </el-form-item>

          <el-form-item label="市" prop="cityId" class="mb8">
            <el-select v-model="queryParams.cityId" placeholder="请选择城市" clearable size="small" @change="changeCity">
              <el-option label="全部" value="" />
              <el-option v-for="un in cityAreas" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
          </el-form-item>

          <el-form-item label="区" prop="districtId" class="mb8">
            <el-select v-model="queryParams.districtId" placeholder="请选择区域" clearable size="small">
              <el-option label="全部" value="" />
              <el-option v-for="un in districtAreas" :value="un.id" :label="un.name" :key="un.id"/>
            </el-select>
          </el-form-item>
          -->


          <el-form-item label="服务区域" prop="districtId" class="mb8">
              <el-cascader :options="areaOptions" :props="searchProps" clearable />
          </el-form-item>
          
        </el-form>
      </div>
      <div class="searchRight">
        <div style="margin-bottom:20px;margin-top:5px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>

    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vanger:engineerUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vanger:engineerUser:remove']"
        >删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['vanger:engineerUser:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" ></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" border :data="engineerUserList" @selection-change="handleSelectionChange"
    	 :default-sort="defaultSort" @sort-change="handleSortChange" :height="bigTableData">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="师傅ID" align="left" prop="code" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="真实姓名" align="left" prop="realName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="头像" align="left" prop="avatar" :show-overflow-tooltip="true" min-width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="left" prop="mobile" :show-overflow-tooltip="true" min-width="100"/>
      
      <el-table-column label="服务区域" align="left" prop="serverZoneName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="服务类型" align="left" prop="serverTypeName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="产品类型" align="left" prop="productCategoryName" :show-overflow-tooltip="true" min-width="100"/>
      

      <el-table-column label="账号状态" align="left" prop="status" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value=1
                :inactive-value=0
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
      </el-table-column>
      <el-table-column label="服务状态" align="left" prop="serviceState" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_engineer_server_status" :value="scope.row.serviceState"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="left" prop="createTime" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="操作" align="left" fixed="right" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['vanger:engineerUser:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['vanger:engineerUser:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工程师用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="showDetail">
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入师傅姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>

        <el-form-item label="服务区域" prop="serverZoneIds">
            <el-cascader :options="areaOptions" :props="formProps" v-model="form.serverZoneIds" clearable />
        </el-form-item>

        <el-form-item label="服务类型" prop="serverTypeIds">
          <el-select v-model="form.serverTypeIds" multiple placeholder="请输入服务类型" clearable >
            <el-option v-for="un in serverTypes" :value="un.id" :label="un.name" :key="un.id"/>
          </el-select>
        </el-form-item>

        <el-form-item label="产品类型" prop="productCategoryIds">
          <el-select v-model="form.productCategoryIds" multiple placeholder="请输入产品类型" clearable >
            <el-option v-for="un in productCategorys" :value="un.id" :label="un.name" :key="un.id"/>
          </el-select>
        </el-form-item>


        <el-form-item label="服务状态" prop="serviceState">
          <el-radio-group v-model="form.serviceState">
            <el-radio
              v-for="dict in dict.type.sk_engineer_server_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="账号状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sk_common_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!showDetail">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEngineerUser, listUnion, getEngineerUser, getInfoUnion, delEngineerUser, addEngineerUser, updateEngineerUser, exportEngineerUser, changeStatus } from "@/api/vanger/engineerUser";
import { getServerTypes, getProductCategorys, getAreas, getAreaOptions } from "@/api/vanger/skcommon";

export default {
  name: "EngineerUser",
  dicts: ['sk_common_status', 'sk_engineer_server_status'],
  
  data() {
    return {
      // 表格高度
      bigTableData:'',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程师用户表格数据
      engineerUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查看详情
      showDetail: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 查询参数
      queryParams: {
        params: {},
        pageNum: 1,
        pageSize: 10,
        code: null,
        userName: null,
        realName: null,
        password: null,
        avatar: null,
        mobile: null,
        isBind: null,
        serviceState: null,
        signImg: null,
        status: null,
        isDelete: null,
        //省区域 
        provinceId: null,
        //市区域 
        cityId: null,
        //区区域 
        districtId: null,
        //产品类型
        productCategoryId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realName: [
            { required: true }
          ],
        mobile: [
          { required: true }
        ],
        serverZoneIds: [
            { required: true }
          ],
        serverTypeIds: [
            { required: true }
          ],
        productCategoryIds: [
            { required: true }
          ]
      },

      //服务状态
      serverTypes: [], 
      //产品类型
      productCategorys: [],
      //省区域 
      provinceAreas: [],
      //市区域 
      cityAreas: [],
      //区区域 
      districtAreas: [],


      searchProps: { multiple: false },
      formProps: { multiple: true },
      areaOptions: []

    };
  },
  created() {
    this.getServerTypeList();
    this.getProductCategoryList();
    this.getProvinceAreaList();
    this.getAreaOptionList();
    this.handleSortChange(this.defaultSort);
  },
  mounted() {
    this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
  },
  watch: {
    showSearch() {
      this.$nextTick(function () {
        this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
      })
    }
  },
  methods: {

    getAreaOptionList(){
      getAreaOptions().then(response => {
        this.areaOptions = response.data;
      });
    },

    getServerTypeList(){
      getServerTypes().then(response => {
        this.serverTypes = response.data;
      });
    },

    getProductCategoryList(){
      getProductCategorys().then(response => {
        this.productCategorys = response.data;
      });
    },

    getProvinceAreaList(){
      getAreas(100000).then(response => {
        this.provinceAreas = response.data;
      });
    },

    getCityAreaList(parentId){
      getAreas(parentId).then(response => {
        this.cityAreas = response.data;
      });
    },

    getDistrictAreaList(parentId){
      getAreas(parentId).then(response => {
        this.districtAreas = response.data;
      });
    },


    changeProvince(){
      this.getCityAreaList(this.queryParams.provinceId);
    },

    changeCity(){
      this.getDistrictAreaList(this.queryParams.cityId);
    },

  
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 查询工程师用户列表 */
    getList() {
      this.loading = true;
      listEngineerUser(this.queryParams).then(response => {
        this.engineerUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.closeDialog()
    },
	// 关闭时设置
    closeDialog() {
      this.open = false;
      this.showDetail = false;
      this.reset();
    },


// 用户状态修改
    handleStatusChange(row) {
      let text = row.status === 1 ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.realName + '"用户吗？').then(function() {
        return changeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0;
      });
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        userName: null,
        realName: null,
        password: null,
        avatar: null,
        mobile: null,
        isBind: null,
        serviceState: null,
        signImg: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {};
      this.resetForm("queryForm");
      // this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增师傅";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEngineerUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑师傅";
      });
    },
    
    /** 查看按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getEngineerUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看工程师用户";
        this.showDetail = true;
      });
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form);
          if (this.form.id != null) {
            updateEngineerUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEngineerUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('确认删除该条师傅信息吗？').then(function() {
        return delEngineerUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vanger/engineerUser/export', {
        ...this.queryParams
      }, `engineerUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
